package com.fh.controller.system.weixin;

import com.fh.controller.base.BaseController;
import com.fh.service.attachment.AttachmentService;
import com.fh.service.building.contract.ContractService;
import com.fh.service.contract.cuser.CUserService;
import com.fh.service.weixin.WeixinService;
import com.fh.util.Const;
import com.fh.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by caozengling on 2018/3/30.
 */
@Controller
@RequestMapping(value = "/WeixinConController")
public class WeixinConController extends BaseController {

    @Autowired
    private CUserService cUserService;
    @Autowired
    private WeixinService weixinService;
    @Autowired
    private ContractService contractService;
    @Resource(name = "attachmentService")
    private AttachmentService attachmentService;

    /**
     * 通过用户获取合同里列表
     */
    @RequestMapping(value = "/weixin_con_list")
    @ResponseBody
    public PageData con_list(HttpServletRequest request) {
        logBefore(logger, "通过用户获取合同里列表");
        PageData res = new PageData();
        PageData pd = this.getPageData();
        try {
            HttpSession session = request.getSession();
            PageData map = (PageData) session.getAttribute(Const.SESSION_PORTAL_USER);
            pd.put("OPEN_ID", map.get("OPEN_ID"));
            // pd.put("OPEN_ID","oV_bU1SFGN-1-vRahsiKZHKmVtHw");
            List<PageData> list = weixinService.con_list(pd);
            res.put("data", list);
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "0");
            res.put("errmsg", e.toString());
        }
        return res;
    }


    /**
     * 通过签订人获取所有合同
     */
    @RequestMapping(value = "/landfindbyname")
    @ResponseBody
    public PageData landfindbyname() {
        logBefore(logger, "通过签订人获取所有的合同");
        PageData res = new PageData();
        PageData pd = this.getPageData();
        try {
            //pd.put("USER_ID", user.getUSER_ID());
            List<PageData> list = weixinService.landfindbyname(pd);
            res.put("data", list);
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "1");
            res.put("errmsg", e.toString());
        }
        return res;
    }


    /**
     * 根据合同ID获取合同详情信息
     */
    @RequestMapping(value = "/con_all")
    @ResponseBody
    public PageData contractInfo() {
        logBefore(logger, "根据合同ID获取合同详情信息");
        PageData res = new PageData();
        PageData pd = this.getPageData();
        try {
            PageData data = new PageData();
            PageData contractData = contractService.findById(pd);
            List<PageData> roomData = contractService.findRoomByContractId(pd);
            List<PageData> contactData = contractService.findContactByContractId(pd);
            List<PageData> bondData = contractService.findBondByContractId(pd);
            List<PageData> discountData = contractService.findDiscountByContractId(pd);
            List<PageData> billData = contractService.findBillByContractId(pd);
            List<PageData> propertyBillData = contractService.findPropertyBillByContractId(pd);
            List<PageData> propertyData = contractService.findPropertyByContractId(pd);
            List<PageData> attachmentData = attachmentService.findByOId(pd);
            List<PageData> throwLeaseData = contractService.findThrowLease(pd);
            List<PageData> initReadData = contractService.findInitRead(pd);
            PageData collectionData = contractService.findCollectionByContractId(pd);

            data.put("contract", contractData);
            data.put("room", roomData);
            data.put("contact", contactData);
            data.put("bond", bondData);
            data.put("discount", discountData);
            data.put("bill", billData);
            data.put("propertyBill", propertyBillData);
            data.put("property", propertyData);
            data.put("attachment", attachmentData);
            data.put("throwLease", throwLeaseData);
            data.put("initRead", initReadData);
            data.put("collectionData", collectionData);
            res.put("data", data);
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "1");
            res.put("errmsg", e.toString());
        }
        return res;
    }
}
